package com.bs.areport.service.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.bs.areport.service.master.UserManageDao;
import com.bs.restframework.context.util.SystemContextUtil;
import com.bs.restframework.db.Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.crypto.Data;

import org.apache.poi.hssf.record.formula.functions.Now;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EntityTypeCreator {
	


	/**
	 * @param args
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws ParseException {
////		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/applicationContext.xml");
////		UserManageDao userDao=(UserManageDao) applicationContext.getBean("usermanagedao");
////		
////		userDao.Create();
//		
////		  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
////		   
////		    String tmpDate = format.format(new Date());
////		    
////		    Date s=format.parse(tmpDate);
////		       
////		    Calendar cal = new GregorianCalendar();   
////		       
////		    cal.set(s.getYear(), s.getMonth(), s.getDay());   
////		       
////		    System.out.println(tmpDate);
////		    
////		    System.out.println(cal.get(Calendar.DAY_OF_WEEK));
		    Calendar now = Calendar.getInstance();
		    String time = now.get((Calendar.YEAR)) + "";
		    time += "年";
		    time += now.get(Calendar.MONTH) + 1;
		    time += "月";
		    time += now.get(Calendar.DATE);
		    time += "日";

		    String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

		    int w = now.get(Calendar.DAY_OF_WEEK) - 1;
		    if (w < 0)
		      w = 0;
		    System.out.print(time + " " + weekDays[w]);
	}
	
	public void Create(){
		Database db=SystemContextUtil.getDatabase(this);
		String sql="select table_Name from user_tables";
		List<String> tblist = db.queryList(String.class, sql,null);
		List<String> collist=null;
		
		System.out.println("/**");
		System.out.println("  * <p>表数据类型定义</p>");
		System.out.println("  */");
		for(int i=0;i<tblist.size();i++){
			System.out.println("public static final char "+tblist.get(i).toUpperCase()+" = "+i+";");
			System.out.println("");
			System.out.println("public static final String "+tblist.get(i).toUpperCase()+"_TABLE = \""+tblist.get(i).toUpperCase()+"\";");
			System.out.println("");
			System.out.println("public static  Map<String, String> "+tblist.get(i).toUpperCase()+"_MAP = new HashMap<String, String>();");
			System.out.println("static {");
			sql="select (CONCAT(A.column_name,'|')||CONCAT(A.data_type,'|')||CONCAT(A.data_length,'|')||CONCAT(A.data_precision,'|')||CONCAT(A.Data_Scale,'|')||CONCAT(A.nullable,'|')||CONCAT(B.comments,'|')) AS ColInfo"+
            " from  user_tab_columns A,user_col_comments B where a.COLUMN_NAME=b.column_name and    A.Table_Name = B.Table_Name and A.Table_Name='"+tblist.get(i).toUpperCase()+"'";
			collist=db.queryList(String.class, sql,null);
			for(int g=0;g<collist.size();g++){
			    System.out.println("    "+tblist.get(i).toUpperCase()+"_MAP.put(\""+collist.get(g).split("\\|")[0].toUpperCase()+"\", \""+collist.get(g).toUpperCase()+"\");");
			}
			System.out.println("}");
			System.out.println("");
		}
		
		System.out.println("/**");
		System.out.println("  *");
		System.out.println("  * <p>函数名称：getTableName</p>");
		System.out.println("  * <p>功能说明：获取数据源表名</p>");
		System.out.println("  * <p>参数说明：type: 数据对象类型</p>");
		System.out.println("  * <p>创建时间：2012-03-07</p>");
		System.out.println("  * <p>作者：李新宇</p>");
		System.out.println(" */");
		System.out.println("public static String getTableName(char type) {");
		System.out.println("");
		System.out.println("    String tableNameString=\"\";");
		System.out.println("");
		System.out.println("    switch (type) {");
		for(int i=0;i<tblist.size();i++){
		    System.out.println("        case "+tblist.get(i).toUpperCase()+":");
		    System.out.println("        tableNameString="+tblist.get(i).toUpperCase()+"_TABLE;");
		    System.out.println("        break;");
		}
		System.out.println("    }");
		System.out.println("    return tableNameString;");
		System.out.println("}");
		
		System.out.println("/**");
		System.out.println("  *");
		System.out.println("  * <p>函数名称：getTypeMap</p>");
		System.out.println("  * <p>功能说明：获取表字段与客户端参数字段对应列表</p>");
		System.out.println("  * <p>参数说明：type: 数据对象类型</p>");
		System.out.println("  * <p>创建时间：2012-03-07</p>");
		System.out.println("  * <p>作者：李新宇</p>");
		System.out.println(" */");
		System.out.println("public static Map<String, String> getTypeMap(char type) {");
		System.out.println("");
		System.out.println("    Map<String, String> reMap=null;");
		System.out.println("");
		System.out.println("    switch (type) {");
		for(int i=0;i<tblist.size();i++){
		    System.out.println("        case "+tblist.get(i).toUpperCase()+":");
		    System.out.println("        reMap="+tblist.get(i).toUpperCase()+"_MAP;");
		    System.out.println("        break;");
		}
		System.out.println("    }");
		System.out.println("    return reMap;");
		System.out.println("}");
	}

}
